/// @tags:
#include <cstdio>
#include <cstring>
#include <iostream>
#define rint register int

using std::cin;
using std::cout;

namespace BlueQuantum {

typedef long long LL;

constexpr int BIT = 15;
constexpr LL MAXN = 1e18;

LL len[BIT], num[BIT], f[BIT][16];

inline int query(LL n) {
  int bcnt = 0;
  while (len[bcnt] <= n) bcnt++;
  n -= len[bcnt - 1];
  LL pos = num[bcnt - 1];
  pos += n / bcnt;
  n -= n / bcnt * bcnt;
  if (!n)
    return (pos - 1) % 16;
  else {
    int i = bcnt - n;
    return (pos >> (i * 4)) & 15;
  }
}

inline LL query(LL n, int c) {
  int bcnt = 0;
  while (len[bcnt] <= n) bcnt++;
  n -= len[bcnt - 1];
  LL pos = num[bcnt - 1];
  pos += n / bcnt;
  n -= n / bcnt * bcnt;
  LL res = 0;
  for (int i = 0; i < bcnt; i++) res += f[i][c];
  LL t = pos - num[bcnt - 1], r = 0;
  for (int i = 0; i < bcnt - 1; i++) {
    res += (t / 16 + (t % 16 > c)) << (i * 4);
    if (t % 16 == c) res += r;
    r += (t % 16LL) << (i * 4);
    t /= 16;
  }
  if (c) {
    res += (LL)(t % 16 >= c) << (bcnt * 4 - 4);
    if ((t % 16 == c - 1)) res += r;
  }
  for (int i = bcnt - 1; (bcnt - i <= n); i--) {
    int t = (pos >> (i * 4)) & 15;
    if (t == c) res++;
  }
  return res;
}

inline int toInteger(char c) { return (c <= '9') ? (c - '0') : (c - 'A' + 10); }

inline char toChar(int x) { return (x <= 9) ? (x + '0') : (x - 10 + 'A'); }

inline int main() {
  num[0] = 1;
  for (int i = 1; len[i - 1] < MAXN; ++i) {
    num[i] = num[i - 1] * 15;
    len[i] = len[i - 1] + num[i] * i;
    f[i][0] = num[i] / 16 * (i - 1);
    for (int c = 1; c < 16; ++c) f[i][c] = num[i] / 15 + num[i] / 16 * (i - 1);
    num[i] += num[i - 1];
  }
  int T;
  cin >> T;
  while (T--) {
    int typ;
    LL n;
    cin >> typ >> n;
    if (typ == 1)
      cout << toChar(query(n)) << '\n';
    else {
      char c;
      cin >> c;
      cout << query(n, toInteger(c)) << '\n';
    }
  }
  return 0;
}

}  // namespace BlueQuantum

int main() {
#ifndef ONLINE_JUDGE
#ifdef LOCAL
  freopen("/tmp/CodeTmp/testdata.in", "r", stdin);
  freopen("/tmp/CodeTmp/testdata.out", "w", stdout);
#else
  freopen("hex.in", "r", stdin);
  freopen("hex.out", "w", stdout);
#endif
#endif

  std::ios::sync_with_stdio(false), cin.tie(NULL), cout.tie(NULL);
  return BlueQuantum::main();
}
